/*
 ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1

 * The contents of this file are subject to the Mozilla Public License Version 
 * 1.1 (the "License"); you may not use this file except in compliance with 
 * the License. You may obtain a copy of the License at 
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Blazing Games Inc. (Billy D. Spelchan) code.
 *
 * The Initial Developer of the Original Code is
 * Blazing Games Inc.
 * Portions created by the Initial Developer are Copyright (C) 1999, 2003, 2005-2006
 * the Initial Developer. All Rights Reserved.
 * 
 * Contributor(s):
 *
 ***** END LICENSE BLOCK *****
 */
package com.blazinggames.holeman;

//import java.applet.*;
import java.awt.*;
//import java.awt.image.*;
import java.awt.event.*;


public class Messages
extends Panel
implements MouseListener, KeyListener {
// *** CONSTANTS ***
  public static final int MSG_INVALIDMAP = 0;
  public static final int MSG_WIN = 1;
  public static final int MSG_DIE = 2;
  public static final int MSG_RESET = 3;
  public static final int MSG_SETWIN = 4;
  public static final int MSG_GAMEOVER = 5;
  public static final int MSG_TRAILER = 7;

  private final static Font fontMSG = new Font("TimesRoman", Font.BOLD, 28);
  private final static Font fontVERSION = new Font("TimesRoman", Font.BOLD, 18);

  private final static String sMSG[] = {
    "*** INVALID MAP PARAMETER ***",
    "LEVEL COMPLETE!",
    "You became lunch!",
    "Level Abandoned!",
    "Set Completed!",
    "GAME OVER!"
  };

  private final static Color colorMSG = Color.yellow;
  private final static Color colorBYLINE = Color.red;
//  private final static Color colorVERSION = Color.white;

// *** VARIABLES ***
	ActionListener eventActionListener;
  Map map;

  int nMessage;

// *** CONSTRUCTORS ***
	public Messages(Map m, ActionListener al) {
//    int cntr;

    map = m;

    setBackground(Color.black);
    // Use the zone adapter as the mouse listeners
		addMouseListener(this);
    addKeyListener(this);
    eventActionListener = al;
    nMessage = 0;
	}

  public void setMessage(int nMsg) {
    nMessage = nMsg;
    repaint();
  }

  public void paintTrailer(Graphics g) {
    int nLvl = map.nCurLevel;
    Color oldColor = g.getColor();
    Font oldFont = g.getFont();
    FontMetrics fm;
    String sTemp;

    g.setFont(fontMSG);
    fm = g.getFontMetrics();
    g.setColor(colorMSG);
    g.drawString(map.sCaption[nLvl], 320 - (fm.stringWidth(map.sCaption[nLvl])>>1), 120);
    g.setColor(colorBYLINE);
    g.drawString(map.sAuthor[nLvl], 320 - (fm.stringWidth(map.sAuthor[nLvl])>>1), 260);

    sTemp = "by";
    g.setFont(fontVERSION);
    fm = g.getFontMetrics();
    g.setColor(Color.gray);
    g.drawString(sTemp, 320 - (fm.stringWidth(sTemp)>>1), 200);
    sTemp = "version: " + map.sVersion[nLvl];
    g.setColor(Color.gray);
    g.drawString(sTemp, 320 - (fm.stringWidth(sTemp)>>1), 330);

    g.setColor(oldColor);
    g.setFont(oldFont);
  }
  // Displays the contents of the panel.  Most work handled by zone adapter
	public void paint(Graphics g) {
    requestFocus();
    if (nMessage == MSG_TRAILER)
      paintTrailer(g);
    else {
      Color oldColor = g.getColor();
      Font oldFont = g.getFont();
      FontMetrics fm;

      g.setFont(fontMSG);
      fm = g.getFontMetrics();
      g.setColor(colorMSG);
      g.drawString(sMSG[nMessage], 320 - (fm.stringWidth(sMSG[nMessage])>>1), 250);

      g.setColor(oldColor);
      g.setFont(oldFont);
    }
	}

// *** MOUSE EVENTS ***
  /**
    Mouse Clicked event from MouseListener.
    Invoked when the mouse has been clicked on a component.
  */
  public void mouseClicked(MouseEvent e) { /* ignore */ }

  /**
    mousePressed event from MouseListener.
    Invoked when a mouse button has been pressed on a component.
  */
  public void mousePressed(MouseEvent e) { /* ignore */ }

 /**
    mouseReleased event from MouseListener.
    Invoked when a mouse button has been released on a component.
  */
  public void mouseReleased(MouseEvent e) {
    keyOrMouseUsed();
  }

  /**
    mouseEntered event from MouseListener.
    Invoked when the mouse enters a component.
  */
  public void mouseEntered(MouseEvent e) { /* ignore */ }

 /**
    mouseExited event from MouseListener.
    Invoked when the mouse exits a component.
  */
  public void mouseExited(MouseEvent e) { /* ignore */ }


  /**
   * Invoked when a key has been typed.
   * This event occurs when a key press is followed by a key release.
   */
  public void keyTyped(KeyEvent e) {/* IGNORED */}

  /**
   * Invoked when a key has been pressed.
   */
  public void keyPressed(KeyEvent e) {
    keyOrMouseUsed();
  }

  /**
   * Invoked when a key has been released.
   */
  public void keyReleased(KeyEvent e) {
  }

  private void keyOrMouseUsed() {
    if (nMessage == MSG_TRAILER)
      eventActionListener.actionPerformed(
            new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Play"));
    else if ( (nMessage == MSG_GAMEOVER) || (nMessage == MSG_SETWIN) )
      eventActionListener.actionPerformed(
            new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Title"));
    else if (nMessage != MSG_INVALIDMAP)
      eventActionListener.actionPerformed(
            new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Next"));
  }
}